package com.springboot.jdbctemplate.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.springboot.jdbctemplate.dao.IMenuDAO;
import com.springboot.jdbctemplate.entity.Menu;

@Repository
public class MenuDAOImpl implements IMenuDAO {
	
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int add(Menu menu) {
		// TODO Auto-generated method stub
		return jdbcTemplate.update("insert into t_menu(f_id) values(?)",
				menu.getId());
	}

	@Override
	public int update(Menu menu) {
		// TODO Auto-generated method stub
		return jdbcTemplate.update("UPDATE  t_menu SET f_name=? WHERE f_id=?",
                menu.getName(), menu.getId());
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return jdbcTemplate.update("DELETE from TABLE t_menu where f_id=?",id);
	}

	@Override
	public Menu findAccountById(String id) {
		// TODO Auto-generated method stub
		 List<Menu> list = jdbcTemplate.query("select * from t_menu where f_id = ?", new Object[]{id}, new BeanPropertyRowMapper(Menu.class));
	        if(list!=null && list.size()>0){
	        	Menu menu = list.get(0);
	            return menu;
	        }else{
	            return null;
	        }
	}

	@Override
	public List<Menu> findAccountList() {
		// TODO Auto-generated method stub
		List<Menu> list = jdbcTemplate.query("select * from t_menu", new Object[]{}, new BeanPropertyRowMapper(Menu.class));
        if(list!=null && list.size()>0){
            return list;
        }else{
            return null;
        }
	}

}
